package com.lcj.eduService.controller;


import com.lcj.commonutils.R;
import com.lcj.eduService.client.VodClient;
import com.lcj.eduService.entity.EduVideo;
import com.lcj.eduService.service.EduVideoService;
import com.lcj.servicebase.config.exceptionHandler.EduException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-14
 */
@RestController
@RequestMapping("/eduService/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    @GetMapping("getVideoInfoById/{id}")
    public R getVideoInfoById(@PathVariable String id){
        EduVideo eduVideo = eduVideoService.getById(id);
        return R.ok().data("video",eduVideo);
    }

    @PostMapping("updateVideo")
    public R updateVideoById(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        //先根据小节id获取视频id
        EduVideo eduVideo =eduVideoService.getById(id);
        String videoId = eduVideo.getVideoSourceId();
        if(!StringUtils.isEmpty(videoId)){
           R result= vodClient.removeVideo(videoId);
           if(result.getCode() == 20001){
               throw new EduException(20001,"删除阿里云视频出错");
           }
        }
        eduVideoService.removeById(id);
        return R.ok();
    }

}

